package travlefate.story.activity;

import android.annotation.TargetApi;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.graphics.Palette;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.joooonho.SelectableRoundedImageView;

import travlefate.story.R;
import travlefate.story.fragment.FragmentCollect;
import travlefate.story.fragment.FragmentFriend;
import travlefate.story.fragment.FragmentMyStory;
import travlefate.story.fragment.FramgentStroy;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    private static final String TAG = "MainActivity";
    private FragmentManager mManager;
    private FragmentTransaction mTransaction;
    private TextView mBarName;
    private ImageView mBarOperation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initToolBar();

        initViews();

        initDatas();

    }

    private void initDatas() {
        mManager = getSupportFragmentManager();
        //初始化第一页面

        mTransaction = mManager.beginTransaction();
        mTransaction.add(R.id.content, new FramgentStroy(), TAG_STORY);
        mTransaction.commitAllowingStateLoss();
    }

    private void initViews() {
        mBarName = (TextView) findViewById(R.id.bar_name);
        mBarOperation = (ImageView) findViewById(R.id.bar_operation);
    }


    /**
     * 初始化ToolBar
     */
    @TargetApi(Build.VERSION_CODES.M)
    private void initToolBar() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);


        initNavigationHeadInfo(toolbar, navigationView);
    }

    /**
     * 动态设置标题色  图标  文字
     *
     * @param toolbar
     * @param navigationView
     */
    private void initNavigationHeadInfo(Toolbar toolbar, NavigationView navigationView) {
        View headerView = navigationView.getHeaderView(0);
        LinearLayout nav_head = (LinearLayout) headerView.findViewById(R.id.nav_head);

        SelectableRoundedImageView mNavHeadUser = (SelectableRoundedImageView) headerView.findViewById(R.id.nav_head_user);

        View headUser = headerView.findViewById(R.id.nav_head_user);


        headUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                startActivity(intent);
            }
        });

        mNavHeadUser.setImageResource(R.mipmap.ic_launcher);

        if (nav_head != null) {
            BitmapDrawable background = (BitmapDrawable) nav_head.getBackground();
            int color = Palette.from(background.getBitmap()).generate().getLightMutedColor(getResources().getColor(R.color.barColor));
            toolbar.setBackgroundColor(color);
        }
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
//            System.exit(0);
        }
    }


    @Override
    public void finish() {
        super.finish();
    }


    private static final String TAG_STORY = "TAG_STORY", TAG_FRIEND = "TAG_FRIEND", TAG_COLLECT = "TAG_COLLECT", TAG_HISTORY = "TAG_HISTORY";
    private Fragment fragmentStory, fragmentFriend, fragmentCollect, fragmentHistory;

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {

        mTransaction = mManager.beginTransaction();

        //故事
        fragmentStory = mManager.findFragmentByTag(TAG_STORY);
        //好友
        fragmentFriend = mManager.findFragmentByTag(TAG_FRIEND);
        //收藏
        fragmentCollect = mManager.findFragmentByTag(TAG_COLLECT);
        //历史
        fragmentHistory = mManager.findFragmentByTag(TAG_HISTORY);


        int id = item.getItemId();

        if (id == R.id.nav_story || id == R.id.nav_friend || id == R.id.nav_mystory || id == R.id.nav_collect) {
            //隐藏所有非NULL
            if (fragmentStory != null) {
                mTransaction.hide(fragmentStory);
            }
            if (fragmentFriend != null) {
                mTransaction.hide(fragmentFriend);
            }
            if (fragmentCollect != null) {
                mTransaction.hide(fragmentCollect);
            }
            if (fragmentHistory != null) {
                mTransaction.hide(fragmentHistory);
            }


        }

        if (id == R.id.nav_story) {
            mBarName.setText(R.string.bar_story);
            mBarOperation.setVisibility(View.VISIBLE);
            mBarOperation.setImageResource(R.mipmap.ic_edit);
            if (fragmentStory == null) {
                mTransaction.add(R.id.content, new FramgentStroy(), TAG_STORY);
            } else {
                mTransaction.show(fragmentStory);
            }
        } else if (id == R.id.nav_friend) {
            mBarName.setText(R.string.bar_friend);
            mBarOperation.setVisibility(View.VISIBLE);
            mBarOperation.setImageResource(R.drawable.ic_friend_add);
            if (fragmentFriend == null) {
                mTransaction.add(R.id.content, new FragmentFriend(), TAG_FRIEND);
            } else {
                mTransaction.show(fragmentFriend);
            }
        } else if (id == R.id.nav_mystory) {
            mBarName.setText(R.string.bar_mystory);
            mBarOperation.setVisibility(View.VISIBLE);
            mBarOperation.setImageResource(R.mipmap.ic_edit);
            if (fragmentHistory == null) {
                mTransaction.add(R.id.content, new FragmentMyStory(), TAG_HISTORY);
            } else {
                mTransaction.show(fragmentHistory);
            }
        } else if (id == R.id.nav_collect) {
            mBarName.setText(R.string.bar_collect);
            mBarOperation.setVisibility(View.GONE);
            mBarOperation.setImageResource(R.mipmap.ic_edit);
            if (fragmentCollect == null) {
                mTransaction.add(R.id.content, new FragmentCollect(), TAG_COLLECT);
            } else {
                mTransaction.show(fragmentCollect);
            }
        } else if (id == R.id.nav_happy) {
            startActivity(new Intent(this, HappyActivity.class));
        } else if (id == R.id.nav_beautiful) {
            startActivity(new Intent(this, BeautifulActivity.class));
        } else if (id == R.id.nav_setting) {
            startActivity(new Intent(this, SettingActivity.class));
        }
        mTransaction.commitAllowingStateLoss();
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}
